# small helper functions go here

from numpy import *


def read_file(name):
    "read a file into a string"
    f = open(name)
    x = f.read()
    f.close()
    return x
    
    
def write_file(name, x):
    "write string into a file"
    f = open(name, "w")
    f.write(x)
    f.close()
    
    
def mk_frame(position, target, up):
    "create camera frame"
    def normalize(v):
        return v / sqrt(dot(v, v))
        
    def cross(a, b):
        return array([a[1]*b[2] - a[2]*b[1], a[2]*b[0] - a[0]*b[2], a[0]*b[1] - a[1]*b[0]])
    
    z = normalize(target - position)
    x = normalize(cross(z, up))
    y = normalize(cross(z, x))
    
    return r_[x, y, z, position]


def find_index(p, l):
    "find first index in l that satisfies predicate p"
    for i, x in enumerate(l):
        if p(x):
            return i
    return None
    
    
